package main.java.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import main.java.domain.User;

public class XMLUtils {
	public static List<User> getUsers(){
		List<User> users=new ArrayList<User>();
		
		InputStream inputStream=XMLUtils.class.getClassLoader().getResourceAsStream("main/resources/users.xml");
		SAXReader reader=new SAXReader();
		Document doc=null;
		try {
			doc=reader.read(inputStream);
			System.out.println("users.xml加载成功。");
		} catch (DocumentException e) {
			System.out.println("users.xml加载失败。");
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		Iterator<?> root=doc.getRootElement().elementIterator();
		while(root.hasNext()) {
			Element e=(Element) root.next();
			String tel=e.attributeValue("tel");
			String pwd=e.attributeValue("pwd");
			users.add(new User(tel,pwd));
		}
		
		return users;
	}
	public static void main(String[] args) {
		XMLUtils.getUsers();
	}
}
